package com.its.myalgorithm.day03;

import com.its.myalgorithm.day01.ArrayUtils;

import java.util.Arrays;

/**
 * @projectName: dgl-algorithm
 * @package: com.its.myalgorithm.day03
 * @className: BSNearLeft
 * @author: DONG GL
 * @description: TODO 有序数组中找到 >=num最左的位置
 * @date: 2022/3/31 8:57
 * @version: 1.0
 */
public class BSNearLeft {
    /**
     * @param arr
     * @param num
     * @return
     */
    public static int mostLeftNoLessNumIndex(int[] arr, int num) {
        if (arr == null || arr.length == 0) {
            return  -1;
        }
        int L = 0;
        int R = arr.length-1;
        int ans = -1;
        //-90 -54 -43 10 17 44 75
        while (L <= R) {
            int mid = (L + R) / 2;
            if (arr[mid] >= num) {
                ans = mid;
                R = mid -1;
            }else {
                L = mid+1;
            }
        }
        return ans;
    }

    public static int test(int[] arr, int value) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= value) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 10;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr = ArrayUtils.generateRandomArray(maxSize, maxValue);
            Arrays.sort(arr);
            int value = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
            if (test(arr, value) != mostLeftNoLessNumIndex(arr, value)) {
                ArrayUtils.printArray(arr);
                System.out.println(value);
                System.out.println(test(arr, value));
                System.out.println(mostLeftNoLessNumIndex(arr, value));
                succeed = false;
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");
        //int[] arr = new int[]{-90, -54, -43, 10, 17 ,44, 75};
        //mostLeftNoLessNumIndex(arr, 88);
    }
}
